package com.qingfeng.testandroid12sdkdemo.activity;

import static java.util.Comparator.comparing;

import android.graphics.Color;
import android.os.Build;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 *  JAVA8 Study Node
 */
public class TestClass {



    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        ArrayList<Apple> results = new ArrayList<>();
        for (Apple apple : inventory) {
            if ("green".equals(apple.color)){
                results.add(apple);
            }
        }
        return results;
    }

    public static List<Apple> filterApplesByColor(List<Apple> inventory, String color) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (apple.color.equals(color)) {
                result.add(apple);
            }
        }
        return result;
    }


    public static List<Apple> filterApplesByWight(List<Apple> inventory, int weight) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (apple.weight > weight) {
                result.add(apple);
            }
        }
        return result;
    }


    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate applePredicate){
        ArrayList<Apple> results = new ArrayList<>();
        for (Apple apple : inventory) {
            if (applePredicate.test(apple)) {
                results.add(apple);
            }
        }
        return results;
    }


    public static void prettyPrintApple(List<Apple> inventory, AppleFormatter<Apple> formatter){
        for (Apple apple : inventory) {
            String output = formatter.accept(apple);
            System.out.println(output);
        }
    }


    public static <T> List<T> filter(List<T> list, Predicate<T> p){
        ArrayList<T> result = new ArrayList<>();
        for (T t : list) {
            if (p.test(t)){
                result.add(t);
            }
        }
        return result;
    }

    public void test(List<Apple> menu){
        ArrayList<Apple> lowCaloricApples = new ArrayList<>();
        for (Apple apple : menu) {
            if (apple.getWeight() < 400){
                lowCaloricApples.add(apple);
            }
        }
        Collections.sort(lowCaloricApples, new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return Integer.compare(o1.getWeight(), o2.getWeight());
            }
        });
        ArrayList<String> lowCaloricApplesName = new ArrayList<>();
        for (Apple apple : lowCaloricApples) {
            lowCaloricApplesName.add(apple.color);
        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            List<String> collect = menu.stream() //单核架构执行
                    .filter(d -> d.getWeight() < 400)
                    .sorted(comparing(Apple::getWeight))
                    .map(Apple::getColor)
                    .collect(Collectors.toList());

            List<String> collect1 = menu.parallelStream()//多核架构并行
                    .filter(d -> d.getWeight() < 400)
                    .sorted(comparing(Apple::getWeight))
                    .map(Apple::getColor)
                    .collect(Collectors.toList());
        }

        List<Dish> menus = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH)
        );


    }

    public void testJava8Method(){

    }


    public static void main(String[] args){
        List<Apple> apples = new ArrayList<>();
        apples.add(new Apple(21, "Green"));
        apples.add(new Apple(22, "Red"));
        apples.add(new Apple(23, "Blue"));
        prettyPrintApple(apples, new AppleFancyFormatter());
        filterApples(apples, new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                return "Red".equals(apple.color);
            }
        });
        
        filterApples(apples, (Apple apple) -> "Red".equals(apple.color));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            apples.sort(comparing(Apple::getWeight).reversed());
        }

        List<String> words = Arrays.asList("Java 8", "Lambdas", "In", "Action");
        String[] arrayOfWords = {"Goodbye", "World"};
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            List<Integer> wordLengths = words.stream()
                    .map(String::length)
                    .collect(Collectors.toList());

            Stream<String> stream = Arrays.stream(arrayOfWords);



        }




    }





}




class AppleComparator implements Comparator<Apple> {

    @Override
    public int compare(Apple o1, Apple o2) {
       return Integer.compare(o1.getWeight(),o2.getWeight());
//        return o1.getWeight().compareTo(o2.getWeight());
    }
}

interface Predicate<T> {
    boolean test(T t);
}



interface AppleFormatter<T>{
    String accept(T a);
}


class AppleFancyFormatter implements AppleFormatter<com.qingfeng.testandroid12sdkdemo.activity.Apple> {

    @Override
    public String accept(Apple a) {
        String characteristic = a.weight > 150 ? "heavy" : "light";

        return "A" + characteristic + " " + a.color + " apple";
    }
}

class AppleSimpleFormatter implements AppleFormatter<Apple>{

    @Override
    public String accept(Apple a) {
        return "An apple of " + a.weight + "g";
    }
}



interface ApplePredicate {
    boolean test (Apple apple);
}

class AppleHeavyWeightPredicate implements ApplePredicate {

    @Override
    public boolean test(Apple apple) {
        return apple.weight > 150;
    }
}


class AppleGreenColorPredicate implements ApplePredicate {

    @Override
    public boolean test(Apple apple) {
        return apple.color.equals("Green");
    }
}

class Apple {
    public int weight;
    public String color;

    public Apple(int weight, String color) {
        this.weight = weight;
        this.color = color;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}



class Dish{
    private String name;
    private boolean vegetarian;
    private int calories;
    private Type type;

    public Dish(String name, boolean vegetarian, int calories, Type type) {
        this.name = name;
        this.vegetarian = vegetarian;
        this.calories = calories;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public int getCalories() {
        return calories;
    }

    public Type getType() {
        return type;
    }

    public enum Type {MEAT, FISH, OTHER}

}
